package com.sx.zongzhi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.CompanyAppealSelectAdapter;
import com.sx.zongzhi.bean.CompanyAppealBean;
import com.sx.zongzhi.presenter.CompanyAppealListPest;
import com.sx.zongzhi.utils.AppConstants;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.ICompanyAppealListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 述求选择单位
 */
public class AppealSelectComActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener
,ICompanyAppealListView{

    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private CompanyAppealSelectAdapter selectAdapter;
    private List<CompanyAppealBean.RowsBean> appealBeans=new ArrayList<>();

    /**已选的*/
    private CompanyAppealBean bean;
    /**排除不能选择的*/
    private CompanyAppealBean exceptList;
    private CompanyAppealListPest companyListPest;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.refresh_recyclerview);
        ButterKnife.bind(this);
        setTitleRight(R.string.confirm);
        Bundle bundle = this.getIntent().getExtras();
        bean= (CompanyAppealBean) bundle.getSerializable("COMPANY");
        exceptList= (CompanyAppealBean) bundle.getSerializable("exceptList");
        companyListPest=new CompanyAppealListPest(this);
        initView();
        onRefresh();
    }

    private void initView() {
        if (bean!=null){
            if (bean.getType()==0){
                setTitleName("主办单位");
            }else {
                setTitleName("抄报单位");
            }
        }

        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setColorSchemeResources(AppConstants.colors);
            swipeRefreshLayout.setOnRefreshListener(this);
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        selectAdapter = new CompanyAppealSelectAdapter(this,R.layout.item_event_type_list,appealBeans);
        selectAdapter.setSelect(bean.getRows());
        selectAdapter.setEmptyView(View.inflate(this,R.layout.load_error_view,null));
        recyclerView.setAdapter(selectAdapter);
        selectAdapter.setEnableLoadMore(true);

    }

    public void addList(CompanyAppealBean.RowsBean rowsBean){
        if (bean.getRows()==null){
            List<CompanyAppealBean.RowsBean> rows=new ArrayList<>();
            bean.setRows(rows);
        }
        if (bean.getType()==0){
            bean.getRows().clear();
        }
        bean.getRows().add(rowsBean);
        if (bean.getType()==0){
            submit();
        }
    }

    public void deleteList(CompanyAppealBean.RowsBean rowsBean){
        if (bean.getRows()==null){
            List<CompanyAppealBean.RowsBean> rows=new ArrayList<>();
            bean.setRows(rows);
        }
        for (int i=0;i<bean.getRows().size();i++){
            if (rowsBean.getId().equals(bean.getRows().get(i).getId())){
                bean.getRows().remove(i);
                break;
            }
        }
    }

    private void submit() {
        Intent intent=new Intent();
        intent.putExtra("COMPANY",bean);
        setResult(RESULT_OK,intent);
        finish();
    }

    @OnClick({R.id.tv_right})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.tv_right:
                submit();
                break;
        }
    }



    @Override
    public void onRefresh() {
        loadData();
    }

    private void loadData() {
        String except="";
        if (exceptList!=null&&exceptList.getRows()!=null&&exceptList.getRows().size()>0){
            int count=exceptList.getRows().size();
            for (int i=0;i<count;i++){
                if (i==count-1){
                    except=except+exceptList.getRows().get(i).getId();
                }else {
                    except=except+exceptList.getRows().get(i).getId()+",";
                }
            }
        }
        companyListPest.getCompanyAppealList(except);
    }

    @Override
    public void showError(String message) {
        swipeRefreshLayout.setRefreshing(false);
        ToastUtils.showToast(this,message);
    }

    @Override
    public void getCompanyList(List<CompanyAppealBean.RowsBean> companyBean) {
        swipeRefreshLayout.setRefreshing(false);
       if (companyBean.size()>0) {
           appealBeans.clear();
           appealBeans.addAll(companyBean);
       }
        selectAdapter.notifyDataSetChanged();
    }
}
